package com.hnzq.cloud.sendassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.IO.AddDriverworkIO;
import com.hnzq.cloud.IO.DriverToAreaParam;
import com.hnzq.cloud.IO.SelectDriversIO;
import com.hnzq.cloud.IO.entity.FreDriverwork;
import com.hnzq.cloud.api.FreDriverworkApi;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.sendassembly.service.FreCarService;
import com.hnzq.cloud.sendassembly.service.FreDriverworkService;
import com.hnzq.cloud.util.FreException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tiangle
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/freDriver")
public class FreDriverworkController implements FreDriverworkApi {
    @Autowired
    private FreDriverworkService freDriverworkService;
    @Autowired
    private FreCarService freCarService;
    @Override
    public Result<FreDriverwork> getByUserId(Integer id) throws FreException {
        FreDriverwork byUserId = freDriverworkService.getByUserId(id);
        return Result.newInstance().setData(byUserId).setCode(200);
    }

    @Override
    public Result<FreDriverwork> getById(Integer id) throws FreException {
        FreDriverwork byId = freDriverworkService.getById(id);
        System.out.println(byId);
        return Result.newInstance().setData(byId).setCode(200);

    }

    @Override
    public Result<FreDriverwork> getByArea(Integer id) throws FreException {
        FreDriverwork byArea = freDriverworkService.getByArea(id);
        if (byArea==null)
            throw new FreException("不存在当前区域的司机",500);
        return Result.newInstance().setData(byArea).setData(200);
    }

    @Override
    public Result<List<FreDriverwork>> getDrivers(SelectDriversIO selectDriversIO) {
        QueryWrapper<FreDriverwork> queryWrapper = new QueryWrapper<>();
        if (selectDriversIO.getDriverStatus()!=null){
            queryWrapper.eq("driver_status",selectDriversIO.getDriverStatus());
        }
        if (selectDriversIO.getWarehouseId()!=null){
            queryWrapper.eq("warehouse_id",selectDriversIO.getWarehouseId());
        }
        List<FreDriverwork> list = freDriverworkService.list(queryWrapper);
        return Result.newInstance().setCode(200).setData(list);
    }


    @GlobalTransactional
    @Override
    public Result addDriverwork(AddDriverworkIO addDriverworkIO) throws FreException {
        try {
            freCarService.getByUserId(addDriverworkIO.getUserId());
        } catch (FreException e) {
            throw new FreException("该司机没有车，无法生成工作表",500);
        }
        //判断仓库是否存在
        if (freDriverworkService.addFreDriverwork(addDriverworkIO)) {
            return Result.newInstance().setCode(200).setMessage("添加成功");
        }
        throw new FreException("添加失败",500);
    }

    @Override
    public Result driverToArea(DriverToAreaParam driverToAreaParam) throws FreException {
        if (freDriverworkService.addArea(driverToAreaParam)) {
            return Result.newInstance().setCode(200).setMessage("分配成功");
        }
        return Result.newInstance().setMessage("分配失败").setCode(500);
    }

    @GlobalTransactional
    @Override
    public Result editDriverwork(AddDriverworkIO addDriverworkIO) {
        //判断仓库
        FreDriverwork byId = freDriverworkService.getById(addDriverworkIO.getDriverId());
        byId.setWarehouseId(addDriverworkIO.getWarehouseId());
        QueryWrapper<FreDriverwork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",byId.getDriverId());
        freDriverworkService.update(queryWrapper);
        return Result.newInstance().setMessage("更改成功").setCode(200);
    }
    @GlobalTransactional
    @Override
    public Result editDriverworkStatus(AddDriverworkIO addDriverworkIO) {
//        FreDriverwork byId = freDriverworkService.getById(addDriverworkIO.getDriverId());
//        byId.setDriverStatus(addDriverworkIO.getDriverStatus());
        freDriverworkService.editFreDriverwork(addDriverworkIO);
        return Result.newInstance().setCode(200).setMessage("状态修改成功");
    }

    @Override
    public Result deleteDriverwork(Integer id) throws FreException {
        FreDriverwork byId = freDriverworkService.getById(id);
        if (byId==null){
            throw new FreException("没有该工作记录",500);
        }
        if (byId.getDriverStatus()!=0){
            throw new FreException("该员工记录处于工作中",500);
        }
        QueryWrapper<FreDriverwork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_id",byId.getDriverId());
        if (freDriverworkService.remove(queryWrapper)) {
            return Result.newInstance().setCode(200).setMessage("删除成功");
        }
        throw new FreException("没有该工作记录",500);
    }
}

